import Vue from 'vue';
import Vuex from 'vuex';
import Utils from '@/common/Utils';

Vue.use(Vuex)

const ADD_USER_TOKEN = "ADD_USER_TOKEN";
const ADD_USER = "ADD_USER";
const ADD_MEMBER = "ADD_MEMBER";
const ADD_OUTTIME = "ADD_OUTTIME";
const LOGOUT = "LOGOUT";
const key_user_token = 'lingpin_token_20241004'
const key_user_info = "liangpin_user_info_20241004"
const key_token_expiration = 'liangpin_user_expiration_20241004'

//localStorage 用于长久保存整个网站的数据，保存的数据没有过期时间，直到手动去除。
//localStorage 用于临时保存同一窗口(或标签页)的数据，在关闭窗口或标签页之后将会删除这些数据。
export default new Vuex.Store({
	state: {
		userToken: uni.getStorageSync(key_user_token) || "",
		userInfo: uni.getStorageSync(key_user_info) ? uni.getStorageSync(key_user_info) : {},
		expiration: uni.getStorageSync(key_token_expiration) ? uni.getStorageSync(key_token_expiration) : 0
	},

	mutations: {
		[ADD_OUTTIME](state, value) {
			state.expiration = value;

			console.log("value", value)
			uni.setStorage({
				key: key_token_expiration,
				data: value,
			})
		},

		[ADD_USER_TOKEN](state, value) {
			state.userToken = value;

			console.log("value", value)
			uni.setStorage({
				key: key_user_token,
				data: value,
			})
		},

		[ADD_USER](state, value) {
			state.userInfo = value;
			uni.setStorage({
				key: key_user_info,
				data: value,
			})
		},

		[LOGOUT](state) {
			state.userToken = null;
			state.userInfo = {};
			state.expiration = 0;
			uni.removeStorageSync(key_user_token)
			uni.removeStorageSync(key_user_info)
			uni.removeStorageSync(key_token_expiration)
		}

	}
})